//
//  StatusListViewModel.swift
//  Sinaweibo
//
//  Created by 谌桃 on 15/12/3.
//  Copyright © 2015年 ryan.chen. All rights reserved.
//

import UIKit
import AFNetworking
import SVProgressHUD
import SDWebImage


class StatusListViewModel: NSObject {
    
    class func loadData(max_id:Int, since_id: Int, finished:(statuses:[Status]?) -> ()){
        
        let urlString = "2/statuses/home_timeline.json"
        
        guard let token = UserAccountViewModel().token else{
            SVProgressHUD.showErrorWithStatus(errorTip)
            
            return
        }
        
        var parameters = ["access_token":token]
        
        //如果大于0, 就需要下拉刷新
        if since_id > 0 {
            
            parameters["since_id"] = "\(since_id)"
        }
     
        NetWorkTools.sharedTools.requestJSONDict(.GET, urlString: urlString, parameters: parameters) { (result, error) -> () in
            
            if error != nil {
                
                print("数据类型有误")
                
                finished(statuses: nil)
                return
            }
            
            //as 转换成 数组里面的字典
            guard let array = result!["statuses"] as? [[String:AnyObject]] else {
                
                SVProgressHUD.showErrorWithStatus(errorTip)
                
                finished(statuses: nil)
                return
            }
            
            //实例化容器数组
            var list = [Status]()
            
            //遍历数组, 取出数组里面的每个字典
            for item in array {
                
//                print(item)
                
                //字典转模型, 定义了模型类
                let status = Status(dict: item)
                
                list.append(status)
                
//                print(list)
                
            }
            
            //进行回调
//            finished(statuses: list)
            
            cacheSingleImage(list, finished: finished)
            
        }
    }
    
    //单张图片下载的类方法
   class func cacheSingleImage(array:[Status],finished:(statuses:[Status]?) -> ()){
    
    if array.count == 0{
        
        finished(statuses: array)
        
        return
        }
    
    //实例化群组任务
    let group = dispatch_group_create()
    
    //遍历模型
    for status in array{
    
        //取出模型里面的每张图片的地址
        if let urls = status.pictureURLs{
        
            if urls.count != 1 {
                
                //如果遍历的模型对象中的图片个数不为1, 就跳出当前循环, 继续下次循环
                continue
                }
            
            //取出只有单张图片, 进行处理
            //开始异步任务之前, 必须先进入群组, 有入组就必须配有出组
            dispatch_group_enter(group)
            
            SDWebImageManager.sharedManager().downloadImageWithURL(urls.first, options: [], progress: nil, completed: { (_, _, _, _, _) -> Void in
                
                print("单张图片下载完成")
                
                //出组
                dispatch_group_leave(group)
            })
        }
    }
    
    //统一通知回调
    dispatch_group_notify(group, dispatch_get_main_queue()) { () -> Void in
        
        print("所有图片下载完成")
        
        //执行统一通知回调
        finished(statuses: array)
        }
    }
}
